package com.owen.springboot.smartparkingms.module.app.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.common.util.ExcelExport;
import com.owen.springboot.smartparkingms.common.util.ShiroUtils;
import com.owen.springboot.smartparkingms.module.app.entity.CarOrder;
import com.owen.springboot.smartparkingms.module.app.entity.Park;
import com.owen.springboot.smartparkingms.module.app.mapper.CarOrderMapper;
import com.owen.springboot.smartparkingms.module.app.service.ICarOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.owen.springboot.smartparkingms.module.sys.entity.SysUser;
import com.owen.springboot.smartparkingms.module.sys.mapper.UserParkMapper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@Service
public class CarOrderServiceImpl extends ServiceImpl<CarOrderMapper, CarOrder> implements ICarOrderService {

    @Resource
    private CarOrderMapper carOrderMapper;
    @Resource
    private UserParkMapper userParkMapper;

    @Override
    public Result listPage(CarOrder carOrder) {
        IPage<CarOrder> page = new Page(carOrder.getPageNo(),carOrder.getPageSize());
        SysUser sysUser = ShiroUtils.getUserEntity();
        IPage<Park> list;
        if(sysUser.getUsername().equals("admin")){
            QueryWrapper query = new QueryWrapper();
            query.like("plate_number",carOrder.getDescription());
            if(carOrder.getParkId()!=null){
                query.eq("park_id",carOrder.getParkId());
            }
            query.orderByDesc("gmt_create");
            list = carOrderMapper.selectPage(page,query);
        }else {
            List<Long> parkIdList = userParkMapper.selectparkId(sysUser.getUserId());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("park_id",parkIdList);
            queryWrapper.like("plate_number",carOrder.getDescription());
            if(carOrder.getParkId()!=null){
                queryWrapper.eq("park_id",carOrder.getParkId());
            }
            queryWrapper.orderByDesc("gmt_create");
            list = carOrderMapper.selectPage(page,queryWrapper);
        }
        return Result.ok(list);
    }

    @Override
    public ExcelExport exportData(Long parkId) throws IOException, InvalidFormatException {
        Map<String, Integer> dataMap = new LinkedHashMap<>();
        dataMap.put("order_no", ExcelExport.CELL_ALIGN_CENTER);
        dataMap.put("plate_number", ExcelExport.CELL_ALIGN_CENTER);
        dataMap.put("total_Fee", ExcelExport.CELL_ALIGN_CENTER);
        dataMap.put("type", ExcelExport.CELL_ALIGN_CENTER);
        dataMap.put("gmt_create", ExcelExport.CELL_ALIGN_CENTER);
        List<Map<String, Object>> list;
        if(parkId!=null){
            list = carOrderMapper.selectOrderByparkId(parkId);
        }else{
            list = carOrderMapper.selectOrder();
        }
        /**
         * 开始生成模板、导出数据
         */
        InputStream stream = ClassUtils.getDefaultClassLoader()
                .getResourceAsStream("static/excelTemplate/orderExport.xls");
        ExcelExport excelExport = new ExcelExport(
                FileUtil.writeFromStream(stream, new File("excelTemplate/orderExport.xls")), 1);
        excelExport.setDataList(list, dataMap, false, "");
        return excelExport;
    }
}
